import 'dart:convert';

import 'package:dio/dio.dart';


/// The [MyResponse] class contains the payload (could be transformed)
/// that respond from the request, and other information of the MyResponse.
///
/// The object is not sealed or immutable, which means it can be manipulated
/// in anytime, typically by [Interceptor] and [Transformer].
class MyResponse<T> extends Response<T>{
  MyResponse({
    this.data,
     this.requestOptions,
    this.statusCode,
    this.statusMessage,
    this.isRedirect = false,
    this.redirects = const [],
    Map<String, dynamic> extra,
    this.code,
    this.status,
    this.msg,
    this.message,
    Headers headers,
  })  : headers = headers ??
            Headers(preserveHeaderCase: requestOptions.preserveHeaderCase),
        extra = extra ?? <String, dynamic>{};

  /// The MyResponse payload in specific type.
  ///
  /// The content could have been transformed by the [Transformer]
  /// before it can use eventually.
  T data;

  /// The [RequestOptions] used for the corresponding request.
  RequestOptions requestOptions;

  /// The HTTP status code for the MyResponse.
  ///
  /// This can be null if the MyResponse was constructed manually.
  int statusCode;

  /// Returns the reason phrase associated with the status code.
  String statusMessage;

  /// Headers for the MyResponse.
  Headers headers;

  /// Whether the MyResponse has been redirected.
  ///
  /// The field rely on the implementation of the adapter.
  bool isRedirect;

  /// All redirections happened before the MyResponse respond.
  ///
  /// The field rely on the implementation of the adapter.
  List<RedirectRecord> redirects;

  /// Return the final real request URI (may be redirected).
  ///
  /// Note: Whether the field is available depends on whether the adapter
  /// supports or not.
  Uri get realUri =>
      redirects.isNotEmpty ? redirects.last.location : requestOptions.uri;

  /// An extra map that you can save your custom information in.
  ///
  /// The field is designed to be non-identical with
  /// [Options.extra] and [RequestOptions.extra].
  Map<String, dynamic> extra;

  //状态码
  final dynamic code;
  final dynamic status;

  //状态描述
  final String msg;
  final String message;

  @override
  String toString() {
    if (data is Map) {
      // Log encoded maps for better readability.
      return json.encode(data);
    }
    return data.toString();
  }
}
